import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import postCssPxToRem from "postcss-pxtorem";
import legacy from "@vitejs/plugin-legacy";

// https://vite.dev/config/
export default defineConfig({
  base: "./",
  plugins: [
    vue(),
    legacy({
      targets: ["defaults", "not IE 11"],
      additionalLegacyPolyfills: ["regenerator-runtime/runtime"],
    }),
    createSvgIconsPlugin({
      iconDirs: [path.resolve(process.cwd(), "src/assets/icons")],
      symbolId: "icon-[dir]-[name]",
    }),
  ],
  resolve: {
    // ↓路径别名
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },
  css: {
    postcss: {
      plugins: [
        postCssPxToRem({
          rootValue: 192, // 按照自己的设计稿修改 1920/10
          unitPrecision: 5, // 保留到5位小数
          selectorBlackList: ["ignore"], // 忽略转换正则匹配项
          propList: ["*", "!border"],
          replace: true,
          mediaQuery: false, // 媒体查询( @media screen 之类的)中不生效
          minPixelValue: 1, //可以选择px小于1的不会被转换
        }),
      ],
    },
  },
  server: {
    proxy: {
      "/dev-api": {
        target: "http://hg-digital-customs.hg.ep.cn",
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/dev-api/, ""),
        //查看真实地址
        // bypass(req, res, options) {
        //   //@ts-ignore
        //   const proxyUrl =
        //     new URL(options.rewrite(req.url) || "", options.target)?.href || "";
        //   console.log(proxyUrl);
        //   req.headers["x-req-proxyUrl"] = proxyUrl;
        //   res.setHeader("x-res-proxyUrl", proxyUrl);
        // },
      },
    },
    port: 8008
  },

  build: {
    outDir: "dist",
    assetsDir: "assets",
  },
});
